package com.fqgj.sentry.manage;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.manage.dao.McApplicationDao;
import com.fqgj.sentry.manage.dao.McGroupManagerDao;
import com.fqgj.sentry.manage.dao.McRoleDao;
import com.fqgj.sentry.manage.dao.McUserDao;
import com.fqgj.sentry.manage.entity.McGroupManagerEntity;
import com.fqgj.sentry.manage.entity.McUserEntity;
import com.fqgj.sentry.manage.enums.UserLevelEnums;
import com.fqgj.sentry.manage.service.GroupManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-13 下午4:59
 */

@Service
public class GroupManagerServiceImpl implements GroupManagerService {

    @Autowired
    private McGroupManagerDao groupManagerDao;
    @Autowired
    private McApplicationDao applicationDao;
    @Autowired
    private McUserDao mcUserDao;

    @Override
    public List<McGroupManagerEntity> selectListByUserId(Long userId) {

        List<McGroupManagerEntity> groupManagerEntities = groupManagerDao.selectListByUserId(userId);
        if (CollectionUtils.isEmpty(groupManagerEntities)) {
            throw new BizException(ErrorCodeEnums.app_group_manager_not_exist_error);
        }
        return groupManagerEntities;
    }

    @Override
    public Boolean addGroupManager(Long merchantId, Long userId, List<Long> rolesId, Long appId) {
        McUserEntity userEntity = mcUserDao.selectByPrimaryKey(userId);
        if (null == userEntity || userEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.user_not_exist_error);
        }
        if (userEntity.getLevel().equals(UserLevelEnums.merchant_admin.getLevel())) {
            throw new BizException(ErrorCodeEnums.merchant_level_error, "系统管理员有所有权限，无需增加权限组");
        }
        if (!userEntity.getMerchantId().equals(merchantId)) {
            throw new BizException(ErrorCodeEnums.merchant_user_error, "当前操作用户的商户内，没有您要添加的用户");
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("appId", appId);
        McGroupManagerEntity groupManagerEntity = groupManagerDao.selectByUserIdAndAppId(param);

        if (null != groupManagerEntity) {
            throw new BizException(ErrorCodeEnums.app_group_manager_is_exit_error, applicationDao.selectByPrimaryKey(appId).getAppName() + "已经存在权限组");
        }
        String rolesIds = "";
        for (int i = 0, size = rolesId.size(); i < size; i++) {
            if (i != (size - 1)) {
                rolesIds += rolesId.get(i) + ",";
            } else {
                rolesIds += rolesId.get(i);
            }
        }
        McGroupManagerEntity mcGroupManagerEntity = new McGroupManagerEntity()
                .setAppId(appId)
                .setRolesId(rolesIds)
                .setUserId(userId);
        groupManagerDao.insert(mcGroupManagerEntity);
        return true;
    }

    @Override
    public Boolean deleteGroupManager(Long groupManagerId) {
        McGroupManagerEntity entity = groupManagerDao.selectByPrimaryKey(groupManagerId);
        if (null == entity || entity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.app_group_manager_not_exist_error);
        }
        groupManagerDao.deleteByPrimaryKey(groupManagerId);
        return true;
    }

    @Override
    public Boolean updateGroupManager(Long groupManagerId, List<Long> rolesId) {
        McGroupManagerEntity entity = groupManagerDao.selectByPrimaryKey(groupManagerId);
        if (null == entity || entity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.app_group_manager_not_exist_error);
        }
        String rolesIdS = "";
        for (int i = 0, size = rolesId.size(); i < size; i++) {
            if (i != (size - 1)) {
                rolesIdS += rolesId.get(i) + ",";
            } else {
                rolesIdS += rolesId.get(i);
            }
        }
        entity.setRolesId(rolesIdS);
        groupManagerDao.updateByPrimaryKey(entity);
        return true;
    }

    @Override
    public McGroupManagerEntity selectByUserIdAndAppId(Long userId, Long appId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("appId", appId);
        return groupManagerDao.selectByUserIdAndAppId(param);
    }
}
